using System;
using System.Collections.Generic;
using System.Text;
using Impact.Model.Objects;
using SubSonic;
using Impact.Infrastructure.State;

namespace Impact.Model.Service
{
    /// <summary>
    /// data service that provides page data
    /// </summary>
    public class PageService : IPageService
    {
        #region IPageService Members

        /// <summary>
        /// retrieve the given page by ID
        /// </summary>
        public ImpactPage FetchByID( int pageId )
        {
            return ImpactPage.FetchByID( pageId );
        }

        /// <summary>
        /// retrieve all pages
        /// </summary>
        /// <param name="onlyEnabled">true to only retrieve the pages that are enabled, 
        /// false to retrieve all pages regardless of their enabled state</param>
        public ImpactPageCollection FetchAllPages( bool onlyEnabled )
        {
            ImpactPageCollection pages = new ImpactPageCollection()
                .Where( ImpactPage.Columns.Type, Comparison.NotEquals, PageType.Template.TypeId )
                .Where( ImpactPage.Columns.Type, Comparison.NotEquals, PageType.Profile.TypeId )
                .OrderByAsc( ImpactPage.Columns.Sequence );
            if ( onlyEnabled )
            {
                pages.Where( ImpactPage.Columns.Enabled, 1 );
            }
            pages.Load();
            //    .LoadDeep(true);

            return pages;
        }

        /// <summary>
        /// Gets the list of ImpactPages that have this ImpactPage as their parent
        /// </summary>
        /// <param name="parentPageId">ID of the parent page to retrieve children for.  
        /// use -1 to get top level pages</param>
        /// <param name="onlyEnabled">true to retrieve only enabled child pages, false to retrieve all child pages</param>
        public ImpactPageCollection FetchChildPages( int parentPageId, bool onlyEnabled )
        {
            ImpactPageCollection children = new ImpactPageCollection()
                    .Where( ImpactPage.Columns.ParentPageId, parentPageId )
                    .Where( ImpactPage.Columns.Type, Comparison.NotEquals, PageType.Template.TypeId )
                    .OrderByAsc( ImpactPage.Columns.Sequence );
            if ( onlyEnabled )
            {
                children.Where( ImpactPage.Columns.Enabled, 1 );
            }
            children.Load();

            return children;
        }

        /// <summary>
        /// retrieve a page using the given key
        /// </summary>
        public ImpactPage FetchByKey( string pageKey )
        {
            ImpactPage page = new ImpactPage();
            page.LoadByParam( ImpactPage.Columns.PageKey, pageKey );
            if ( page.PageId > 0 ) return page;
            else return null;
        }

        /// <summary>
        /// retrieve all ImpactPage objects that are marked as templates
        /// </summary>
        public TemplateCollection FetchAllTemplates()
        {
            return new TemplateCollection().Load();
        }

        /// <summary>
        /// save the given page with the given userId as responsible for the changes
        /// </summary>
        public void Save( ImpactPage page, Guid userId )
        {
            page.Save( userId );
        }

        /// <summary>
        /// saves the given page without a responsible user
        /// </summary>
        public void Save( ImpactPage page )
        {
            page.Save();
        }

        /// <summary>
        /// deletes the given page from the system, and any items referencing it
        /// </summary>
        public void Remove( int pageId )
        {
            ImpactPage.Delete( pageId );
        }

        /// <summary>
        /// retrieves any pages that have a title or body text that is similar to the terms given
        /// </summary>
        public ImpactPageCollection FetchByTerms( string terms )
        {
            QueryCommand qc = new QueryCommand( "SELECT * FROM impact_Pages WHERE Enabled='true' AND ( Title LIKE '%'+ @terms +'%' OR Body LIKE '%'+ @terms +'%' )", Databases.ImpactProvider );
            qc.AddParameter( "@terms", terms, System.Data.DbType.String );

            ImpactPageCollection pages = new ImpactPageCollection();
            pages.LoadAndCloseReader( DataService.GetReader( qc ) );
            return pages;
        }


        #endregion
    }
}
